var e = require("../../requests/request.js"), a = (require("../../requests/newapi.js").getPhoneNumber, 
require("../../requests/newapi.js")), t = a.postEnquireOrder, s = a.decryptUserInfo, d = getApp();

Page({
    data: {
        carid: 0,
        dealerid: 0,
        dealername: "",
        Image: "",
        carname: "",
        username: "",
        usertel: "",
        cityid: 0,
        errmsg: "",
        subordertype: 0,
        successhidden: !0,
        dealerInfoList: [],
        isshowdealerList: !0,
        isshowheadmsg: !0,
        dealerlist: [],
        phoneauthmsg: "",
        btntxt: "获取底价"
    },
    onLoad: function(e) {
        var a = this;
        a.data.Image = decodeURIComponent(e.img), a.data.dealername = decodeURIComponent(e.dealername), 
        a.data.carname = decodeURIComponent(e.carname), a.data.dealerid = e.dealerid, a.data.carid = e.carid, 
        a.data.cityid = e.cityid, a.data.subordertype = e.subordertype, a.data.dealerid > 0 && (a.data.isshowdealerList = !1), 
        a.data.errmsghidden = !0, console.log("isshowdealerList:" + e.dealerid), 0 == a.data.subordertype ? wx.setNavigationBarTitle({
            title: "询底价"
        }) : (wx.setNavigationBarTitle({
            title: "贷款"
        }), a.setData({
            btntxt: "申请贷款"
        })), a.setData({
            Image: decodeURIComponent(e.img),
            dealername: decodeURIComponent(e.dealername),
            carname: decodeURIComponent(e.carname),
            dealerid: e.dealerid,
            carid: e.carid,
            cityid: e.cityid,
            errmsghidden: !0,
            isshowheadmsg: a.data.isshowheadmsg,
            isshowdealerList: a.data.isshowdealerList
        }), a.data.isshowdealerList && a.setDealerData();
    },
    successCallback: function(e) {
        var a = this, t = e.Data;
        a.data.isshowheadmsg = !1, a.data.dealerInfoList = t.List, a.dealerlist = new Array();
        var s = 0;
        for (var d in a.data.dealerInfoList) {
            var r = "4S店" === a.data.dealerInfoList[d].DealerBizModeName;
            if (a.data.dealerInfoList[d].Is4S = r, void 0 != a.data.dealerInfoList[d].DealerBizModeName && a.data.dealerInfoList[d].DealerBizModeName.length > 2 && (a.data.dealerInfoList[d].DealerBizModeName = a.data.dealerInfoList[d].DealerBizModeName.substring(0, 2), 
            a.data.dealerInfoList[d].DealerTel = a.data.dealerInfoList[d].DealerTel.replace(/转/, ","), 
            a.data.dealerInfoList[d].DealerTelShow = a.data.dealerInfoList[d].DealerTel.split(",")[0]), 
            void 0 != a.data.dealerInfoList[d].DealerID) {
                var i = new Object();
                i.Index = s, i.DealerID = a.data.dealerInfoList[d].DealerID, i.DealerBizModeName = a.data.dealerInfoList[d].DealerBizModeName, 
                i.DealerName = a.data.dealerInfoList[d].DealerName, i.SaleRegion = a.data.dealerInfoList[d].SaleRegion, 
                i.DealerSaleAddr = a.data.dealerInfoList[d].DealerSaleAddr, i.DealerTel = a.data.dealerInfoList[d].DealerTel, 
                i.SaleRegion = a.data.dealerInfoList[d].SaleRegion, i.IsSelect = s < 3, a.dealerlist[s] = i, 
                s++;
            }
        }
        void 0 == a.data.dealerInfoList || s < 1 ? (console.log(s), a.data.isshowdealerList = !1, 
        a.data.dealerInfoList = []) : a.data.isshowdealerList = !0, a.setData({
            dealerInfoList: a.data.dealerInfoList,
            isshowdealerList: a.data.isshowdealerList,
            isshowheadmsg: a.data.isshowheadmsg,
            dealerlist: a.dealerlist
        });
    },
    bindlistclick: function(e) {
        var a = this, t = e.currentTarget, s = parseInt(t.dataset.id), d = a.dealerlist;
        d[s].IsSelect = !d[s].IsSelect, console.log(d[s].IsSelect), a.setData({
            dealerlist: d
        });
    },
    bindUserNameInput: function(e) {
        this.setData({
            username: e.detail.value,
            errmsghidden: !0
        });
    },
    bindUserTelInput: function(e) {
        this.setData({
            usertel: e.detail.value,
            errmsghidden: !0
        });
    },
    getPhoneNumber: function(e) {
        var a = this, t = e.detail, d = t.iv, r = t.encryptedData, i = wx.getStorageSync("session_key");
        d && r ? (wx.showLoading(), s({
            op: "encrypteddata",
            iv: d,
            encrypteddatastr: r,
            sessionkey: i
        }).then(function(e) {
            wx.hideLoading(), a.setData({
                usertel: e.phoneNumber
            });
        }).catch(function(e) {
            wx.showToast({
                title: "获取失败",
                icon: "none"
            }), a.setData({
                phoneauthmsg: "deny"
            });
        })) : this.setData({
            phoneauthmsg: "deny"
        });
    },
    telDealer: function(e) {
        console.log("正在拨打电话" + e.currentTarget.dataset.tel), wx.showModal({
            title: "拨打电话",
            content: "电话号码：" + e.currentTarget.dataset.tel,
            success: function(a) {
                a.confirm && wx.makePhoneCall({
                    phoneNumber: e.currentTarget.dataset.tel
                });
            }
        });
    },
    bindPostOrder: function() {
        var e = this, a = this, s = a.data.dealerid, d = wx.getStorageSync("UserToken");
        if (a.data.isshowdealerList) {
            s = "";
            var r = 0;
            for (var i in a.data.dealerlist) a.data.dealerlist[i].IsSelect && (s += a.data.dealerlist[i].DealerID + ",", 
            r++);
            if (r < 1) return void a.setmsg("至少选择一个经销商询价");
            if (r > 5) return void a.setmsg("最多只能选5家经销商询价");
            s = s.substr(0, s.length - 1);
        } else if (s < 1) return void a.setmsg("至少选择一个经销商询价");
        a.checkusername(a.data.username) && a.checkPhone(a.data.usertel) && (wx.showLoading(), 
        t({
            carid: this.data.carid,
            cityid: this.data.cityid,
            dealerids: s,
            username: this.data.username,
            usertel: this.data.usertel,
            subordertype: this.data.subordertype,
            token: d
        }).then(function(a) {
            Array.isArray(a) ? (wx.showToast({
                title: "提交成功",
                mask: !0
            }), setTimeout(wx.navigateBack, 1500)) : (wx.hideLoading(), e.setData({
                errmsghidden: !1,
                errmsg: a.Message
            }));
        }).catch(function(a) {
            wx.hideLoading(), e.setmsg("提交失败，请稍等再试");
        }));
    },
    setmsg: function(e) {
        var a = this;
        a.setData({
            errmsg: e,
            errmsghidden: !1
        }), setTimeout(function() {
            a.setData({
                errmsghidden: !0
            });
        }, 2e3);
    },
    checkusername: function(e) {
        var a = this;
        return !!/^[\一-\龥]{1,6}$/.test(e) || (a.setmsg("姓名要求1-6个汉字"), !1);
    },
    checkPhone: function(e) {
        var a = this;
        return !!/^1[34578]\d{9}$/.test(e) || (a.setmsg("手机号填写错误"), !1);
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
    bindJumpInfo: function() {
        console.log("go info"), wx.navigateTo({
            url: "../info/info"
        });
    },
    setDealerData: function() {
        var a = this;
        e.getDealerInfoList(a.data.carid, a.data.cityid, function(e) {
            a.successCallback(e);
        }, function(e) {
            console.log(e);
        }, function(e) {
            console.log(e);
        });
    },
    returnBtn: function() {
        d.gotoIndex();
    }
});